﻿<?php if (!defined('SERVER_ROOT')) exit('No direct script access allowed');

class nhomquyen_Model extends Model {

    function __construct()
    {
        parent::__construct();
    }
    public function qry_all_nhomquyen()
    {
        return $this->db->ExecuteCursor("BEGIN PHANQUYEN.SELECT_NHOM_QUYEN(:CUR); END;",'CUR');
    }
    public function qry_single_nhomquyen($MA_NHOM_QUYEN)
    {
        return $this->db->ExecuteCursor("BEGIN PHANQUYEN.SELECT_NHOM_QUYEN_DETAIL(:CUR,'$MA_NHOM_QUYEN'); END;",'CUR');
    }
    public function update_nhomquyen()
    {
        $P_UPDATE_TYPE = get_post_var('hdn_update_type');
        $P_NGUOI_CAP_NHAT = Session::get(TIEP_DAU_NGU_SESSION.'MA_CAN_BO');
        $P_MA_NHOM_QUYEN = get_post_var('txt_MA_NHOM_QUYEN');
        $P_TEN_NHOM_QUYEN = get_post_var('txt_TEN_NHOM_QUYEN');
        $P_STT_NHOM_QUYEN = get_post_var('txt_STT_NHOM_QUYEN');
        $P_TRANG_THAI = get_post_var('cbx_TRANG_THAI');

        $stmt = $this->db->PrepareSP("BEGIN PHANQUYEN.UPDATE_NHOM_QUYEN(:P_VAL,:P_UPDATE_TYPE,:P_NGUOI_CAP_NHAT,:P_MA_NHOM_QUYEN, :P_TEN_NHOM_QUYEN,:P_STT_NHOM_QUYEN,:P_TRANG_THAI);END;");
        $this->db->OutParameter($stmt,$P_VAL,'P_VAL');
        $this->db->InParameter($stmt,$P_UPDATE_TYPE,'P_UPDATE_TYPE');
        $this->db->InParameter($stmt,$P_NGUOI_CAP_NHAT,'P_NGUOI_CAP_NHAT');
        $this->db->InParameter($stmt,$P_MA_NHOM_QUYEN,'P_MA_NHOM_QUYEN');
        $this->db->InParameter($stmt,$P_TEN_NHOM_QUYEN,'P_TEN_NHOM_QUYEN');
        $this->db->InParameter($stmt,$P_STT_NHOM_QUYEN,'P_STT_NHOM_QUYEN');
        $this->db->InParameter($stmt,$P_TRANG_THAI,'P_TRANG_THAI');
        $chaythutuc = $this->db->Execute($stmt);
        //echo '<script>alert("'.$P_VAL.'");</script>';
        if ($chaythutuc){
          if ($P_VAL == 'THANH_CONG'){
              $this->popup_exec_done();
          }elseif($P_VAL == 'TON_TAI_MA'){
              $this->popup_exec_fail(__('Mã nhóm quyền đã tồn tại!'));
              return;
          }elseif($P_VAL == 'TON_TAI_TEN'){
              $this->popup_exec_fail(__('Tên nhóm quyền đã tồn tại!'));
              return;
          }else{
               $this->popup_exec_fail(__('Không thể cập nhật!'));
               return;
          }
        }else{
            echo $this->db->ErrorMsg();
            $this->popup_exec_fail('Lỗi database');
            return;
        }
    }
    public function delete_nhomquyen(){
        $P_NGUOI_XOA = Session::get(TIEP_DAU_NGU_SESSION.'MA_CAN_BO');
        $P_MA_NHOM_QUYEN = get_post_var('hdn_item_id',0);
        //echo '<script>alert("'.$P_MA_NHOM_QUYEN.'");</script>';
        $stmt = $this->db->PrepareSP("BEGIN PHANQUYEN.DELETE_NHOM_QUYEN(:P_VAL,:P_NGUOI_XOA,:P_MA_NHOM_QUYEN);END;");
        $this->db->OutParameter($stmt,$P_VAL,'P_VAL');
        $this->db->InParameter($stmt,$P_NGUOI_XOA,'P_NGUOI_XOA');
        $this->db->InParameter($stmt,$P_MA_NHOM_QUYEN,'P_MA_NHOM_QUYEN');
        $chaythutuc = $this->db->Execute($stmt);
        if ($chaythutuc){
            if ($P_VAL == 'THANH_CONG'){
                $this->exec_done($this->goback_url);
            }else{
                 $this->exec_fail($this->goback_url, 'Không thể xóa!');
                 return;
            }
        }else{
            echo $this->db->ErrorMsg();
            $this->exec_fail($this->goback_url,'Lỗi database');
            return;
        }
    }
   /****************************form dsp_all_nhomquyen***********************/
    public function SELECT_ALL_TR_QUYEN_CUA_NQ($MA_NHOM_QUYEN){
        return $this->db->ExecuteCursor("BEGIN PHANQUYEN.SELECT_ALL_TR_QUYEN_CUA_NQ(:CUR,'$MA_NHOM_QUYEN'); END;",'CUR');
    }
    public function update_nhomquyencanbo(){
        $P_NGUOI_CAP_NHAT = Session::get(TIEP_DAU_NGU_SESSION.'MA_CAN_BO');
        $P_MA_NHOM_QUYEN  = get_post_var('hdn_item_id',0);
        $stmt = $this->db->PrepareSP("BEGIN PHANQUYEN.DELETE_ALL_NQ_CO_QUYEN(:P_VAL,:P_MA_NHOM_QUYEN);END;");
        $this->db->OutParameter($stmt,$P_VAL,'P_VAL');
        $this->db->InParameter($stmt,$P_MA_NHOM_QUYEN,'P_MA_NHOM_QUYEN');
        $chaythutuc = $this->db->Execute($stmt);
        if ($chaythutuc){
          if ($P_VAL == 'THANH_CONG'){
              $v_phan_quyen       = get_post_var('hdn_phan_quyen','');
              $arr_phan_quyen = explode(',', $v_phan_quyen);
              foreach ($arr_phan_quyen as $P_MA_QUYEN){
                  if ( $P_MA_QUYEN != NULL && $P_MA_QUYEN != ''){
                      $stmt = $this->db->PrepareSP("BEGIN PHANQUYEN.INSERT_NQ_CO_QUYEN(:P_VAL,:P_NGUOI_CAP_NHAT,:P_MA_NHOM_QUYEN,:P_MA_QUYEN);END;");
                      $this->db->OutParameter($stmt,$P_VAL,'P_VAL');
                      $this->db->InParameter($stmt,$P_NGUOI_CAP_NHAT,'P_NGUOI_CAP_NHAT');
                      $this->db->InParameter($stmt,$P_MA_NHOM_QUYEN,'P_MA_NHOM_QUYEN');
                      $this->db->InParameter($stmt,$P_MA_QUYEN,'P_MA_QUYEN');
                      $chaythutuc = $this->db->Execute($stmt);
                      if($P_VAL != 'THANH_CONG'){
                           $this->popup_exec_fail(__('Không cập nhật, vui lòng liên hệ VNPT Tiền Giang: 0949291288!'));
                           return;
                      }
                  }
              }
               $this->popup_exec_done();
          }else{
               echo $this->db->ErrorMsg();
               $this->popup_exec_fail('Lỗi database');
               return;
           }
       }

    }
}